package com.cqjtu.education.service.impl;

import com.cqjtu.education.entity.Chapter;
import com.cqjtu.education.dao.ChapterDao;
import com.cqjtu.education.service.ChapterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Chapter)表服务实现类
 *
 * @author makejava
 * @since 2022-04-20 15:46:19
 */
@Service("chapterService")
@Slf4j
public class ChapterServiceImpl implements ChapterService {
    @Resource
    private ChapterDao chapterDao;

    @Override
    public List<Chapter> queryFuzzy(String courseId, String... chapterId) {
        return this.chapterDao.queryFuzzy(courseId, chapterId);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param chapterId 主键
     * @return 实例对象
     */
    @Override
    public Chapter queryById(String courseId, String chapterId, String sectionId) {
        return this.chapterDao.queryById(courseId, chapterId, sectionId);
    }


    /**
     * 新增数据
     *
     * @param chapter 实例对象
     * @return 实例对象
     */
    @Override
    public Chapter insert(Chapter chapter) {
        try {
            if (this.chapterDao.queryById(chapter.getCourseId(), chapter.getChapterId(), chapter.getSectionId()) == null)
                this.chapterDao.insert(chapter);
        } catch (Exception e) {
            log.info("insert");
            log.error(e.toString());
        }
        return chapter;
    }

    @Override
    public List<Chapter> insertBatch(List<Chapter> chapters) {
        try {
            this.chapterDao.insertBatch(chapters);
        } catch (Exception e) {
            log.info("insertBatch");
            log.error(e.toString());
        }
        return chapters;
    }

    /**
     * 修改数据
     *
     * @param chapter 实例对象
     * @return 实例对象
     */
    @Override
    public Chapter update(Chapter chapter) {
        this.chapterDao.update(chapter);
        return this.queryById(chapter.getCourseId(), chapter.getChapterId(), chapter.getSectionId());
    }

    /**
     * 通过主键删除数据
     *
     * @param chapterId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String courseId, String chapterId, String sectionId) {
        return this.chapterDao.deleteById(courseId, chapterId, sectionId) > 0;
    }

    @Override
    public boolean deleteBatch(List<String> courseId, List<String> chapterId, List<String> sectionId) {
        return this.chapterDao.deleteBatch(courseId, chapterId, sectionId) > 0;
    }
}
